package model.hospital;

import model.UID_Generator;
import net.passClass.PassData;
import net.passClass.PassRoom;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.TreeSet;

public class Room implements Comparable<Room> {
    private String name;
    private Location location;
    private String type;
    private String note;
    private Equipment equipment;
    private TreeSet<Bed> beds = new TreeSet<>();

    public PassData add(Room room) {
        if(equipment!=null){
            return new PassData(false,null,"稀有房间，床位新增失败");
        }
        Bed aBed = room.beds.first();
        if(this.beds!=null? this.beds.contains(aBed): false){
            return new PassData(false,null,"床位已存在，新增失败");
        }
        Bed newBed = new Bed(aBed.getBedNumber());
        newBed.setUid(UID_Generator.getUid("B"));
        if (this.beds == null) {
            this.beds = new TreeSet<Bed>();
        }
        this.beds.add(newBed);
        System.out.println("bed:"+this.beds);
        return new PassData(true,null,"床位新增成功");
    }

    public PassData delete(Room room) {
        Bed theBed = room.beds.first();
        if(!beds.contains(theBed)){
            return new PassData(false, null,"床位不存在，删除失败");
        }
        if(theBed.getPatientUID()!=null){
            return new PassData(false,null,"床位上存在病人，删除失败");
        }
        beds.remove(theBed);
        return new PassData(true,null,"床位删除成功");
    }

    public static PassRoom get(Room room){
        LinkedList<Room> tmpAns = new LinkedList<>();
        for(Building aBuilding: BuildingDB.getInstance().getBuildings()){
            for(Floor aFloor: aBuilding.getFloors()){
                tmpAns.addAll(aFloor.getRooms());
            }
        }

        if(room.beds!=null){
            for(Room aRoom : tmpAns){
                if(aRoom.beds!=null && aRoom.beds.contains(room.beds.first())){
                    ArrayList<Room> ans = new ArrayList<>();
                    ans.add(aRoom);
                    return new PassRoom(true,ans,"查找成功");
                }
            }
            return new PassRoom(false,null,"不存在查找结果");
        }

        if(room.location.getBuildingNumber()!=0){
            tmpAns.removeIf(aRoom -> aRoom.location.getBuildingNumber()
                    !=(room.location.getBuildingNumber()));
        }
        if(room.location.getFloorNumber()!=0){
            tmpAns.removeIf(aRoom -> aRoom.location.getFloorNumber()
                    !=(room.location.getFloorNumber()));
        }
        if(room.location.getRoomNumber()!=0){
            tmpAns.removeIf(aRoom -> aRoom.location.getRoomNumber()
                    !=(room.location.getRoomNumber()));
        }
        if(room.name!=null){
            tmpAns.removeIf(aRoom -> aRoom.name==null ||
                    !aRoom.name.contains(room.name));
        }
        if(room.note!=null){
            tmpAns.removeIf(aRoom -> aRoom.note==null ||
                    !aRoom.note.contains(room.note));
        }
        if(room.type!=null){
            tmpAns.removeIf(aRoom -> aRoom.type==null ||
                    !aRoom.type.contains(room.type));
        }
        if(room.equipment!=null){
            tmpAns.removeIf(aRoom -> aRoom.equipment==null);
        }
        ArrayList<Room> ans = new ArrayList<>(tmpAns);
        return new PassRoom(true, ans,"查找成功");
    }

    @Override
    public int compareTo(Room o) {
        return this.location.getRoomNumber() - o.location.getRoomNumber();
    }

    public Room(int roomNumber, int floorNumber, int buildingNumber) {
        this.location = new Location();
        this.location.setRoomNumber(roomNumber);
        this.location.setFloorNumber(floorNumber);
        this.location.setBuildingNumber(buildingNumber);
        this.name = buildingNumber + "-" +
                floorNumber +
                String.valueOf(roomNumber+100).substring(1);
    }

    public Room(int roomNumber) {
        this.location = new Location();
        this.location.setRoomNumber(roomNumber);
    }

    public Room(String name, Location location, String type, String note,
                Equipment equipment, TreeSet<Bed> beds) {
        this.name = name;
        this.location = location;
        this.type = type;
        this.note = note;
        this.equipment = equipment;
        this.beds = beds;
    }

    public Room() {
    }

    public Equipment getEquipment() {
        return equipment;
    }

    public void setEquipment(Equipment equipment) {
        this.equipment = equipment;
    }

    public TreeSet<Bed> getBeds() {
        return beds;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }



    @Override
    public String toString() {
        return "Room{" +
                "name='" + name + '\'' +
                ", location=" + location +
                ", type='" + type + '\'' +
                ", note='" + note + '\'' +
                ", equipment=" + equipment +
                ", beds=" + beds +
                '}';
    }
}
